package com.advaita.gui;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

import com.advaita.license.Customer;
import com.advaita.license.Selection;
import com.advaita.util.*;

public class ExperimentTab extends JPanel {
	private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
	private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);

	private GridBagConstraints createGbc(int x, int y) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;

		gbc.anchor = (x == 0) ? GridBagConstraints.WEST
				: GridBagConstraints.EAST;
		gbc.fill = (x == 0) ? GridBagConstraints.BOTH
				: GridBagConstraints.HORIZONTAL;

		gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
		gbc.weightx = (x == 0) ? 0.1 : 1.0;
		gbc.weighty = 1.0;
		return gbc;
	}

	ExperimentTab(Customer customer) {

		// removeAll();
		Dimension size = getPreferredSize();
		size.width = 250;
		size.height = 350;
		setPreferredSize(size);

		setBorder(BorderFactory.createTitledBorder(null,
				"Chose The Data Universe", TitledBorder.LEFT, TitledBorder.TOP,
				new Font("Serif", Font.ITALIC + Font.BOLD, 20)));

		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		SetupComboBox setupComboBox = new SetupComboBox();
//		Object[] items = { new ComboItem("A"), new ComboItem("B"),
//				new ComboItem("1", false), new ComboItem("2", false),
//				new ComboItem("abc"), new ComboItem("def") };
		Object[] items1 = new ComboItem[11];
		Object[] items2 = new ComboItem[2];
		Object[] items3 = new ComboItem[6];
		
		int index=0;
		for (Selection organismIterator : customer.licenseInfo.getOrganisms()) {
			System.out.println("Organism : " + organismIterator.getSelection()
					+ "  ( " + organismIterator.getIsEnabled() + " ) " + index);
			items1[index]=new ComboItem(organismIterator.getSelection(), 
								   organismIterator.getIsEnabled() );
			index++;
			//  System.out.println("Current index is: " + (index++));
		}
		
		index = 0;
		for (Selection pathwayDBIterator : customer.licenseInfo.getDatabase()) {
			System.out.println("Pathway DB : "
					+ pathwayDBIterator.getSelection() + "  ( "
					+ pathwayDBIterator.getIsEnabled() + " ) " + index);
			items2[index]=new ComboItem(pathwayDBIterator.getSelection(),
					pathwayDBIterator.getIsEnabled());
			index++;
		}
	
		index = 0;
		for (Selection geneIDIterator : customer.licenseInfo.getGeneID()) {
			items3[index]=new ComboItem(geneIDIterator.getSelection(),
										geneIDIterator.getIsEnabled());
		//	System.out.println("Gene ID : " + geneIDIterator.getSelection()
		//			+ "  ( " + geneIDIterator.getIsEnabled() + " )");
			index++;
		}
		
		
		JComboBox combo = new JComboBox(items1);
		combo.setRenderer(new ComboRenderer());
		combo.addActionListener(new ComboListener(combo));

		JComboBox combo2 = new JComboBox(items2);
		combo2.setRenderer(new ComboRenderer());
		combo2.addActionListener(new ComboListener(combo2));

		JComboBox combo3 = new JComboBox(items3);
		combo3.setRenderer(new ComboRenderer());
		combo3.addActionListener(new ComboListener(combo3));

		JLabelAllinOne label2 = new JLabelAllinOne(
				"   Choose genes monitored in the only experiment.  Do not include genes not measured in the experiment.",
				"Serif", Font.PLAIN, 18);
		add(label2, c);

		// Organism and combo box
		JLabelAllinOne label3 = new JLabelAllinOne("     Organism:       ", "Serif",
				Font.PLAIN, 18);
		
		c = createGbc(0, 1);
		c.anchor=GridBagConstraints.CENTER;
		c.weightx=1;
		c.weighty=1;
		c.fill=GridBagConstraints.BOTH;
		add(label3, c);
		
		c = createGbc(1, 1);
		add(combo, c);
		
		
		// Database and combo box
		JLabelAllinOne label4 = new JLabelAllinOne("     Database:       ", "Serif",
				Font.PLAIN, 18);
		c = createGbc(0, 2);
		add(label4, c);
		c = createGbc(1, 2);
		add(combo2, c);

		JLabelAllinOne label5 = new JLabelAllinOne(
				"     Type of identifier:       ", "Serif", Font.PLAIN, 18);
		c.gridx = 0;
		c.gridy = 4;
		add(label5, c);
		c.gridx = 1;
		c.gridy = 4;
		add(combo3, c);

		// Radio Buttons

		ReferenceSetPanel referenceSetPanel = new ReferenceSetPanel(customer);
		c.gridx = 0;
		c.gridy = 5;
		add(referenceSetPanel, c);

		// Next Button
		JButton nextBtn = new JButton("Next");
		c.gridx = 1;
		c.gridy = 6;
		add(nextBtn, c);
	}
}
